<?php

class AdministrativosController extends AppController {

    public $helpers = array('Html', 'Form');
    var $name = "Administrativos";
    //var $components = array('Auth');

    function index() {
        $this->layout = "layout";
        //paginacion y ordenar
        $this->paginate = array('limit' => 15, 'page' => 1);
        $Administrativos = $this->paginate('Administrativo');
        $this->set(compact('Administrativos'));
        //paginacion y ordenar
    }

    function ver($administrativo_id = null) {
        $this->layout = "layout";
        $administrativo = $this->Administrativo->getInfoAdministrativobyId($administrativo_id);
        $this->set('administrativo', $administrativo[0]);
    }

    function agregar() {
        $this->layout = "layout";
        $this->set("accion", "Agregar");
        if ($this->request->is('post')) {
            if (isset($this->request->data['User']['password2']))
                $this->request->data['User']['password2hashed'] = $this->Auth->password($this->request->data['User']['password2']);
            $this->request->data = Sanitize::clean($this->request->data, array('encode' => false));
            $this->Administrativo->User->create();
            $password = $this->request->data["User"]["password"];
            $password2 = $this->request->data["User"]["password2"];
            $this->request->data["User"]["password"] = $this->Auth->password($this->request->data["User"]["password"]);
            $this->request->data["User"]["tipo"] = 2;
            if ($this->Administrativo->saveAll($this->request->data)) {
                $this->Session->setFlash('Administrador guardado con exito');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->request->data["User"]["password"] = $password;
                $this->request->data["User"]["password2"] = $password2;
                $this->Session->setFlash('No se pudo Guardar el Administrador');
            }
        }
    }

    function modificar($administrativo_id = null) {

        $this->layout = "layout";
        if (empty($this->request->data)) {
            $administrativo = $this->Administrativo->getInfoAdministrativobyId($administrativo_id);
            $this->request->data = $administrativo[0];
        } else {
            $this->Administrativo->User->create();     //para crear un paciente ptimero se regustra un User
            if (!!$this->Administrativo->saveAll($this->request->data)) {  // SaveAll guarda en todas las tablas involucradas
                $this->Session->setFlash('Los datos del Administrador fueron modificado con exito.');
                $this->redirect(array('controller' => 'administrativos', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Los datos del Administrador no se pudieron modificar.');
            }
        }
        $this->set("accion", "Modificar");
    }

    function eliminar($administrativo_id = null) {
        $this->layout = "layout";
        if ($this->Administrativo->delete($administrativo_id))
            $this->Session->setFlash('Administrativo eliminado con exito.');
        else
            $this->Session->setFlash('El administrativo no se pudo eliminar.');
        $this->redirect(array('controller' => 'administrativos', 'action' => 'index'));
    }

     
     function buscar( ){
         $this->layout="layout";
           if($this->request->is('post')){
             $this->Session->setFlash('Personal administrativo encontrado:');
             $this->paginate = array('limit' => 15, 'page' => 1);   
             $administrativos = $this->paginate('Administrativo');
             $datos=$this->Administrativo->BuscarPorNombre($this->data['q']);
             if($datos!=null){
                  $this->set('datos', $datos);
            }
             else{
                 $this->Session->setFlash('No se encontro personal medico con este nombre');
                 $this->redirect(array('controller' => 'administrativos','action' => 'index'));
             }
           }


     }
}
?>

